////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.skins {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.GradientType;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.ComponentSkin;
	import kiwi.components.skins.IButtonSkin;
	
	import plantool.helpers.TextFieldHelper;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class DefaultButtonSkin extends ComponentSkin implements IButtonSkin {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _width:Number			= 0;
		private var _height:Number			= 0;
		private var _text:String			= "";
		private var _textField:TextField	= null;
		private var _textColor:Number		= 0xFFFFFF;
		private var _backgroundColor:Number = 0x000000;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		override public function get text():String				{ return _text; }
		override public function set text(value:String):void 	{ _text = value; invalidate(); }
		
		override public function get height():Number 			{ return _height; }
		override public function set height(value:Number):void 	{ _height = value; invalidate(); }
		override public function get width():Number 			{ return _width; }
		override public function set width(value:Number):void 	{ _width = value; invalidate(); }
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function DefaultButtonSkin() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		override protected function init():void {
			//create a textfield
			_textField = TextFieldHelper.createInfoTf(TextFieldAutoSize.NONE, TextFormatAlign.CENTER, false);
			addChild(_textField);
			
			//super init
			super.init();
		}
		
		override protected function update():void {
			//clear graphics first
			graphics.clear();
			
			//draw background
			graphics.beginFill(_backgroundColor);
			graphics.drawRoundRect(0, 0, _width, _height, 6, 6);
			graphics.endFill();
			
			var myMatrix:Matrix = new Matrix();
			myMatrix.createGradientBox(_width, _height, Math.PI / 2, 0, 0);
			var colors:Array = [0xFFFFFF, 0xFFFFFF];
			colors = [0xFFFFFF, 0xFFFFFF];
			var alphas:Array = [0.2, 0.4];
			var ratios:Array = [0, 255];
			
			graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, myMatrix);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
			//set the text on the textfield
			_textField.width		= _width;
			_textField.text 		= _text;
			_textField.textColor 	= _textColor;
			_textField.y			= (_height - _textField.height) / 2;
		}
		
		public function mouseHandler(event:MouseEvent):void {
			switch (event.type) {
				case MouseEvent.ROLL_OUT:
					_textColor	 		= 0xFFFFFF;
					_backgroundColor	= 0x000000;
					break;
				case MouseEvent.ROLL_OVER:
					_textColor	 		= 0x000000;
					_backgroundColor	= 0xCCCCCC;
					break;
			}
			
			//invalidate to redraw the skin
			invalidate();
		}
	}
}